<template>
  <div class="login-container">
    <div class="login-box">
      <h1 class="title">欢迎注册</h1>
      <el-form :model="data.form" ref="formRef" :rules="data.rules" class="register-form">
        <el-form-item prop="username" class="form-item">
          <el-input :prefix-icon="User" size="large" v-model="data.form.username" placeholder="请输入账号" class="custom-input" />
        </el-form-item>
        <el-form-item prop="nickName" class="form-item">
          <el-input :prefix-icon="User" size="large" v-model="data.form.name" placeholder="请输入昵称" class="custom-input" />
        </el-form-item>
        <el-form-item prop="password" class="form-item">
          <el-input :prefix-icon="Lock" size="large" v-model="data.form.password" placeholder="请输入密码" show-password class="custom-input" />
        </el-form-item>
        <el-form-item prop="confirmPassword" class="form-item">
          <el-input :prefix-icon="Lock" size="large" v-model="data.form.confirmPassword" placeholder="请确认密码" show-password class="custom-input" />
        </el-form-item>
        <el-form-item>
          <el-button size="large" type="primary" class="register-btn" @click="register">注 册</el-button>
        </el-form-item>
      </el-form>
      <div class="login-link">
        已有账号，<router-link to="/login" class="link">去登陆</router-link>
      </div>
    </div>
  </div>
</template>

<script setup>
  import { reactive, ref } from "vue";
  import { User, Lock } from "@element-plus/icons-vue";
  import request from "@/utils/request";
  import {ElMessage} from "element-plus";
  import router from "@/router";
  import {registerAPI} from "@/apis/login";

  const validatePass = (rule, value, callback) => {
    if (!value) {
      callback(new Error('请确认密码'))
    } else if (value !== data.form.password) {
      callback(new Error('两次输入密码不一致'))
    } else {
      callback()
    }
  }

  const data = reactive({
    form: {
      username:'',
      password:'',
      role: 'USER',
      name:''
    },
    rules: {
      username: [
        { required: true, message: '请输入账号', trigger: 'blur' },
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
      ],
      confirmPassword: [
        { validator: validatePass, trigger: 'blur' },
      ],
      name: [
        { required: true, message: '请输入昵称', trigger: 'blur' },
        { min: 1, max: 15, message: '昵称长度在 1 到 15 个字符', trigger: 'blur' }
      ]
    }
  })


  const formRef = ref()

  const register = () => {
    formRef.value.validate((async valid => {
      if (valid) {
        await registerAPI(data.form).then(res=>{
          if(res.code==='200'){
            ElMessage.success('注册成功')
            router.push('/login')
          }else{
            ElMessage.error('注册失败，'+res.msg)
          }
        })
      }
    })).catch(error => {
      console.error(error)
    })
  }

</script>

<style scoped>
.login-container {
  min-height: 100vh;
  overflow: hidden;
  display: flex;
  justify-content: center;
  align-items: center;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  background-size: cover;
}

.login-box {
  width: 400px;
  padding: 40px;
  border-radius: 15px;
  box-shadow: 0 8px 20px rgba(0, 0, 0, 0.2);
  background-color: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  transition: transform 0.3s ease;
}

.login-box:hover {
  transform: translateY(-5px);
}

.title {
  font-size: 28px;
  font-weight: 600;
  color: #2c3e50;
  text-align: center;
  margin-bottom: 35px;
  letter-spacing: 2px;
}

.register-form .form-item {
  margin-bottom: 25px;
}

.custom-input :deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  background-color: #f8fafc;
}

.custom-input :deep(.el-input__wrapper:hover) {
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}
.login-link {
  text-align: center;
  margin-top: 20px;
  color: #606266;
}
.register-btn {
  width: 100%;
  height: 45px;
  border-radius: 8px;
  font-size: 16px;
  font-weight: 500;
  letter-spacing: 2px;
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
  transition: transform 0.2s ease, box-shadow 0.2s ease;
}

.register-btn:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.register-btn:active {
  transform: translateY(0);
}
</style>